//package com.lry.sso.auth.server.filter;
//
//import com.lry.sso.auth.common.constant.LoginAuthorizationConstant;
//import com.lry.sso.auth.common.util.AuthUtil;
//import com.lry.sso.auth.common.util.SessionInfo;
//import com.lry.sso.auth.server.config.AuthConfigProperties;
//import com.lry.sso.auth.server.service.JwtTokenService;
//import com.lry.sso.exception.SsoErrorEnum;
//import com.lry.sso.common.exception.SysInvocationException;
//import com.lry.sso.common.util.PathUtil;
//import com.lry.sso.service.redis.service.RedisService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.Ordered;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
////@Component
////@WebFilter(urlPatterns = "/*")
////@Order(Ordered.HIGHEST_PRECEDENCE + 2)
//public class TokenFilter implements Filter {
//
//    private static final Logger logger = LoggerFactory.getLogger(TokenFilter.class);
//    ;
//
//    @Autowired
//    private JwtTokenService jwtTokenService;
//
//    @Autowired
//    private RedisService redisService;
//
//    @Autowired
//    private AuthConfigProperties authConfigProperties;
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        String token = AuthUtil.getTokenForUrl((HttpServletRequest) request);
//        if (this.ignoreApi((HttpServletRequest) request)) {
//            chain.doFilter(request, response);
//            return;
//        }
//        SessionInfo sessionInfo = AuthUtil.getSessionInfo(token);
//        String key = AuthUtil.getLoginJwtKey(sessionInfo.getUserName());
//        Long expire = null;
//        try {
//            expire = jwtTokenService.getExpire(token);
//        } catch (Exception e) {
//            logger.info("获取token过期时间异常：{}", e.getMessage());
//            e.printStackTrace();
//            throw new SysInvocationException(SsoErrorEnum.TOKEN_PARSE);
//        }
//        logger.info("token过期时间剩余：{}分钟", expire / 60000.0);
//        if (expire < authConfigProperties.getLoginExpire()) {
//            //token剩余时间
//            logger.info("token过期时间小于登录过期时间，设置token过期时间为登录过期时间。");
//            String newToken = null;
//            try {
//                newToken = jwtTokenService.getToken(sessionInfo);
//            } catch (Exception e) {
//                logger.info("生成新的token异常：{}", e.getMessage());
//            }
//            redisService.set(key, newToken, authConfigProperties.getJwtExpire());
//            //同时设置到response中
//            ((HttpServletResponse) response).setHeader(LoginAuthorizationConstant.TOKEN_HEADER, newToken);
//        }
//    }
//
//    /**
//     * 忽略匹配
//     * @param request 请求对象
//     * @return true：忽略
//     */
//    private boolean ignoreApi(HttpServletRequest request) {
//        StringBuffer requestURL = request.getRequestURL();
//        String[] ignoreApi = new String[]{
//                "**/login","**/captcha","**/captcha/validate","**/publicKey"
//        };
//        return PathUtil.match(requestURL.toString(),ignoreApi);
//    }
//
//}
